package lowijs.XfDroid;

import java.util.*;

import android.app.*;
import android.content.*;
import android.graphics.*;
import android.os.*;
import android.view.*;
import android.widget.*;

import lowijs.XfireClient.*;
import lowijs.XfireClient.event.*;

public class Friendslist extends ListActivity implements XfireObserver {
	private final XfireClient xfc = XfireClient.getInstance();
	private final FriendManager fm = FriendManager.getInstance();
	
	private FriendslistAdapter adapter = null;
	
	public void onCreate(Bundle b) {
		super.onCreate(b);
		setContentView(R.layout.friendslist);
	}

	public void onStart() {
		EventManager.addObserver(this);
		friendslistHandler.sendEmptyMessage(0);
		super.onStart();
	}
	
	public void onStop() {
		EventManager.removeObserver(this);
		super.onStop();
	}
	
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK)
			return true;
		return super.onKeyDown(keyCode, event);
	}

	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater mi = getMenuInflater();
		mi.inflate(R.menu.menu_contactlist, menu);
		return true;
	}

	public boolean onOptionsItemSelected(MenuItem item) {
		switch(item.getItemId()) {
		case R.id.menu_contactlist_logoff:
			Intent i = new Intent(this, Login.class);
			startActivity(i);
			finish();
			xfc.disconnect();
			return true;
		}
		return false;
	}

	public void handleEvent(XfireEvent event) {
		switch(event.getEventType()) {
		case XfireEvent.XF_FRSTATUS:
		case XfireEvent.XF_FRGAME:
			friendslistHandler.sendEmptyMessage(0);
			break;
		}
	}
	
	private Handler friendslistHandler = new Handler() {
		public void handleMessage(Message m) {
			if (adapter == null) {
				Context ctx = getBaseContext();
				adapter = new FriendslistAdapter(ctx, R.layout.friendrow,
						fm.getOnlineFriends());
				setListAdapter(adapter);
			} else
				adapter.notifyDataSetChanged();
		}
	};
	
	private class FriendslistAdapter extends ArrayAdapter<Friend> {
		public FriendslistAdapter(Context context, int textViewResourceId,
				List<Friend> objects) {
			super(context, textViewResourceId, objects);
		}

		public View getView(int position, View convertView, ViewGroup parent) {
			View v = convertView;
            if (v == null) {
                LayoutInflater li = (LayoutInflater)getSystemService(
                                Context.LAYOUT_INFLATER_SERVICE);
                v = li.inflate(R.layout.friendrow, null);
            }

            Friend f = getItem(position);
            
            TextView nick = (TextView)v.findViewById(R.id.tvNick);
            TextView statustext = (TextView)v.findViewById(R.id.tvStatusText);
            ImageView imageview = (ImageView)v.findViewById(R.id.ivImage);
            
            nick.setText(f.getNick());
            statustext.setText(f.getStatusText());
            
            byte[] image = f.getImage();
            if (image != null) {
            	Bitmap bm = BitmapFactory.decodeByteArray(image, 0, image.length);
            	imageview.setImageBitmap(bm);
            } else
            	imageview.setImageResource(R.drawable.unknown);
            
            return v;
		}		
	}
}
